package com.hmkj.core.service.amount.impl;

import com.hmkj.common.baseMapper.GenericPo;
import com.hmkj.core.constant.ConfigNID;
import com.hmkj.core.exception.BussinessException;
import com.hmkj.core.mapper.amount.AccountMapper;
import com.hmkj.common.baseMapper.GenericMapper;
import com.hmkj.core.model.amount.AccountModel;
import com.hmkj.core.po.amount.Account;
import com.hmkj.core.service.amount.AccountService;
import com.hmkj.core.service.cache.RedisService;
import com.xiaoleilu.hutool.util.BeanUtil;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * 账户业务相关Service接口实现<br>
 *
 * @author donfy
 * @ClassName: AccountServiceImpl
 * @date 2017-08-15 03:10:41
 */
@Service
@Scope("prototype")
@Transactional(rollbackFor = Exception.class)
public class AccountServiceImpl implements AccountService {

    @Resource
    private AccountMapper accountMapper;
    @Resource
    private RedisService redisService;

    @Override
    public GenericMapper<Account, Integer> _getMapper() {
        return accountMapper;
    }

    @Override
    public AccountModel getAccountModel(Integer userId) {
        Account account = accountMapper.selectOne(new Account(t -> {
            t.setUserId(userId);
            t.setDelFlag(GenericPo.DELFLAG.NORMAL.code);
        }));
        if (account == null) throw new BussinessException("查询用户账户信息失败");

        AccountModel model = new AccountModel();
        BeanUtil.copyProperties(account, model);

        BigDecimal rechargeMin = redisService.get(ConfigNID.RECHARGE_MIN, BigDecimal.class);
        if (rechargeMin == null) rechargeMin = new BigDecimal(10);// 默认单笔充值最低金额为10元
        model.setRechargeMin(rechargeMin);

        BigDecimal rechargeMax = redisService.get(ConfigNID.RECHARGE_MAX, BigDecimal.class);
        if (rechargeMax == null) rechargeMax = new BigDecimal(10000000);// 默认单笔充值最高金额为10000000元
        model.setRechargeMax(rechargeMax);

        BigDecimal cashMin = redisService.get(ConfigNID.CASH_MIN, BigDecimal.class);
        if (cashMin == null) cashMin = new BigDecimal(10);// 默认单笔提现最低金额为10元
        model.setCashMin(cashMin);

        BigDecimal cashMax = redisService.get(ConfigNID.CASH_MAX, BigDecimal.class);
        if (cashMax == null) cashMax = new BigDecimal(50000);// 默认单笔提现最高金额为50000元
        model.setCashMax(cashMax);

        BigDecimal cashFeeMin = redisService.get(ConfigNID.CASH_FEE_MIN, BigDecimal.class);
        if (cashFeeMin == null) cashFeeMin = new BigDecimal(0);// 默认单笔提现最低手续费为0元
        model.setCashFeeMin(cashFeeMin);

        BigDecimal cashFeeRatio = redisService.get(ConfigNID.CASH_FEE_RATIO, BigDecimal.class);
        if (cashFeeRatio == null) cashFeeRatio = new BigDecimal(0);// 默认提现手续费率为 0.0%
        model.setCashFeeRatio(cashFeeRatio);

        return model;
    }

    @Override
    public BigDecimal selectSumAvailableByType(Integer type,Integer agentId,String startTime,String endTime) {
        Map<String, Object> param = new HashMap<>();
        param.put("type",type);
        param.put("startTime",startTime);
        param.put("endTime",endTime);
        param.put("agentId",agentId);
        return accountMapper.selectSumAvailableByType(param);
    }

    @Override
    public BigDecimal selectSumFreezeByType(Integer type,Integer agentId,String startTime,String endTime) {
        Map<String, Object> param = new HashMap<>();
        param.put("type",type);
        param.put("startTime",startTime);
        param.put("endTime",endTime);
        param.put("agentId",agentId);
        return accountMapper.selectSumFreezeByType(param);
    }
}
